<?php


require '../classes/imageClass.php';
require '../classes/albumClass.php';
require'../classes/connectionFactoryClass.php';
require'../classes/imageDao.php';
require'../classes/albumDao.php';
require'../classes/validates.php';
require'checksDate.php';

$album = new AlbumClass();
$image = new ImageClass();
$date = new checksDate();
$c = new ConnectionFactory();
$db = $c->getConnection();
$imageDao = new imageDao($db);
$albumDao = new albumDao($db);

$valid = new validates();
$id = $_GET['id'];


$fotos = $imageDao->getById($id);

if ($fotos) {
    $idAlbum = $fotos->getAlbumId();
    $albuns = $albumDao->getById($idAlbum);

    $directoryTemp = '../' . $albuns->getDirectory() .$albuns->getNameUser().'/'. $albuns->getNameAlbum() . '/' . $fotos->getName_photo();
    $dir = '../' . $albuns->getDirectory() . $albuns->getNameAlbum();
    
    if (unlink($directoryTemp)) {
        $fileexiste = scandir($dir);
        if (!isset($fileexiste[2])) {
            rmdir($dir);
            $albumOk = $albumDao->delete($idAlbum);
        }
        else
            $albumOk = true;
        $imageDeletada = $imageDao->delete($id);
    }else $imageDeletada=false;
} else {
    
}

if ($albumOk && $imageDeletada) {
    $result = true;
}
?>

<script>



    //<meta http-equiv="content-type" content="text/html;charset=utf-8" />;
    var cadTest = <?php echo $result; ?>;


    if (cadTest) {

        alert("Foto deletada com sucesso!");
        setTimeout("document.location = '../index.php'", 500);

    } else {

        alert("Não Foi Possível Deletar essa foto!");
        setTimeout("document.location = '../index.php'", 500);
    }


</script>

